package com.xnx3.wangmarket.plugin.innerDown.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xnx3.wangmarket.plugin.base.controller.BasePluginController;
import com.xnx3.wangmarket.plugin.kefu_domain.entity.Kefu;

import net.sf.json.JSONObject;

import com.xnx3.wangmarket.plugin.kefu_domain.bean.MQBean;
import com.xnx3.Lang;
import com.xnx3.StringUtil;
import com.xnx3.UrlUtil;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.func.AttachmentFile;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.j2ee.vo.UploadFileVO;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.util.AliyunLog;
import com.xnx3.wangmarket.domain.mq.DomainMQ;

/**
 * 下载系统,一些公共的接口（总管理后台、普通用户都可以用的）
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/plugin/innerDown/")
public class InnerDownPluginController extends BasePluginController{
	@Resource
	private SqlService sqlService;
	
	/**
	 * 用户后台页面
	 * 先判断用户拥有总管理权限，如果有，则跳转到总管理后台首页
	 */
	@RequestMapping("index${url.suffix}")
	public String index(Model model){
		if(haveSuperAdminAuth()){
			//有总管理员权限
			return redirect("plugin/innerDown/admin/index.do");
		}
		
		if(!haveUser()){
			//如果未登陆，需要登陆
			return error(model, "请登陆后使用");
		}
		AliyunLog.addActionLog(getSiteId(), "进入下载系统innerDown用户后台首页");
		
		return "plugin/innerDown/index";
	}
	
	/**
	 * 上传文件的接口
	 * @param filename input上传的模版文件的name
	 * @return result:BaseVO.SUCCESS 成功， 则
	 * 				url: 上传文件的绝对url
	 * 				suffix:上传的文件的后缀
	 * 				name: 文件名字（不含后缀）
	 * 				size: 文件大小，单位KB
	 */
	@RequestMapping(value="uploadFile${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public void uploadFile(HttpServletResponse response, HttpServletRequest request, 
			@RequestParam("filename") MultipartFile multipartFile) throws IOException{
		JSONObject json = new JSONObject();
		User user = getUser();
		if(user == null){
			json.put("result", BaseVO.FAILURE);
			json.put("info", "请先登录");
		}else if(multipartFile == null){
			json.put("result", BaseVO.FAILURE);
			json.put("info", "请选择要上传的文件");
		}else{
			UploadFileVO vo = AttachmentFile.uploadFileByMultipartFile("plugin/innerdown/"+user.getId()+"/", multipartFile);
			json.put("result", vo.getResult());
			json.put("info", vo.getInfo());
			if(vo.getResult() - UploadFileVO.SUCCESS == 0){
				//成功
				json.put("url", vo.getUrl());
				json.put("suffix", Lang.findFileSuffix(vo.getFileName()));
				json.put("name", UrlUtil.getFileBeforeName(multipartFile.getOriginalFilename()));
				json.put("size", vo.getSize());
			}else{
				//失败
			}
		}
		
		response.setCharacterEncoding("UTF-8");  
	    response.setContentType("application/json; charset=utf-8");  
	    PrintWriter out = null;  
	    try { 
	        out = response.getWriter();  
	        out.append(json.toString());
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    } finally {  
	        if (out != null) {  
	            out.close();  
	        }
	    }  
	}
}
